/***************************************************************************
*   Copyright (C) 2011 by Chuck Gao & Alex Liu                             *
*                                                                          *
*   This program is free software: you can redistribute it and/or modify   *
*   it under the terms of the GNU General Public License as published by   *
*   the Free Software Foundation, either version 3 of the License, or      *
*   (at your option) any later version.                                    *
*                                                                          *
*   This program is distributed in the hope that it will be useful,        *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
*   GNU General Public License for more details.                           *
*                                                                          *
*   You should have received a copy of the GNU General Public License      *
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
*                                                                          *
*   Contact author: chuckgao.cg@gmail.com                                  *
*                   alex2202375@gmail.com                                  *
***************************************************************************/

import QtQuick 1.0
import "../Script/createCuisineDetailedView.js" as CuisineDetailedViewCreator

Item {
    id: cuisineSelectView
    anchors.fill: parent
    smooth: true

    property bool showDetailed: false
    property QtObject recipe: null

    function goNextCuisineNode(myID) {
        if(showDetailed)
        {
            showDetailed = false;
            recipe = engine.recipe(myID)
            animationText.text = recipe.name
            showDetailedAnimation.running = true
            return;
        }
        cuisineListView.currentIndex = myID;
        changeViewAnimation.running = true
    }

    NumberAnimation {
        id: changeViewAnimation
        target: cuisineListView; property: "opacity"; to: 0; duration: 400
        running: false
        onCompleted: {
            var id = cuisineListView.currentIndex;

            if(engine.hasChildCuisine(id))
            {
                cuisineListView.model = engine.cuisineModel(id)
            }else{
                cuisineListView.model = engine.recipeModel(id)
                showDetailed = true
            }
            showViewAnimation.running = true
        }
    }

    NumberAnimation {
        id: showViewAnimation
        target: cuisineListView; property: "opacity"; to: 1; duration: 400
        running: false
    }

    Text {
        id: animationText
        width: parent.width
        height: 80
        x: -animationText.width/2
        anchors.verticalCenter: parent.verticalCenter
        visible: showDetailedAnimation.running
        font.family: "Freestyle Script"
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        font.pointSize: 56
        color: "white"
        opacity: 0.8
    }

    ParallelAnimation {
        id: showDetailedAnimation
        running: false
        NumberAnimation {
            target: cuisineListView; property: "x"; to: -cuisineListView.width; duration: 400
        }
        NumberAnimation {
            target: animationText; property: "x"; to: (cuisineListView.width - animationText.width)/2; duration: 800
        }
        NumberAnimation {
            target: animationText; property: "opacity"; to: 0; duration: 800
        }

        onCompleted: {
            CuisineDetailedViewCreator.createCuisineDetailedView(recipe)
        }
    }

    /*******************************************
     * Test code -- Enter
     *****************************************/
//    ListModel {
//        id: cuisineModel
//        ListElement {
//            cuisineName: "川菜";
//        }
//        ListElement {
//            cuisineName: "粤菜";
//        }
//        ListElement {
//            cuisineName: "鲁菜";
//        }
//        ListElement {
//            cuisineName: "湘菜";
//        }
//        ListElement {
//            cuisineName: "闽菜";
//        }
//        ListElement {
//            cuisineName: "浙菜";
//        }
//        ListElement {
//            cuisineName: "苏菜";
//        }
//        ListElement {
//            cuisineName: "徽菜";
//        }
//    }

//    ListModel {
//        id: cuisineNode1

//        ListElement {
//            cuisineName: "回锅肉"; cuisineIndex: 0
//        }
//        ListElement {
//            cuisineName: "麻婆豆腐"; cuisineIndex: 1
//        }
//        ListElement {
//            cuisineName: "鱼香肉丝"; cuisineIndex: 2
//        }
//        ListElement {
//            cuisineName: "宫保鸡丁"; cuisineIndex: 3
//        }
//        ListElement {
//            cuisineName: "金沙玉米"; cuisineIndex: 4
//        }
//        ListElement {
//            cuisineName: "苦瓜炒蛋"; cuisineIndex: 5
//        }
//        ListElement {
//            cuisineName: "红烧肉"; cuisineIndex: 6
//        }
//    }
    /*******************************************
     * Test code -- Exit
     *****************************************/

    Component {
        id: myDelegate
        CuisineItem {
            cuisineItemName: name
            cuisineID: _ROWID_
        }
    }

    ListView {
        id: cuisineListView
        width: 360
        height: 640
        smooth: true
        clip: true
        focus: true
        preferredHighlightBegin: 0.5
        preferredHighlightEnd: 0.5
        highlightRangeMode: ListView.StrictlyEnforceRange
        highlightFollowsCurrentItem: false
        model: engine.cuisineModel(-1) // -1 to get the root cuisine
        delegate: myDelegate
    }
}
